from rest_framework import serializers
from .models import Appointment


class AppointmentCreateSerializer(serializers.Serializer):
    """预约创建序列化器 - 只接收必要的ID信息"""
    patient_id = serializers.IntegerField()
    doctor_id = serializers.IntegerField()
    time_slot_id = serializers.IntegerField()
    
    def validate(self, data):
        """验证各项ID是否有效"""
        from .utils import get_doctor_info, get_time_slot_info
        
        # 验证医生ID
        doctor_info = get_doctor_info(data['doctor_id'])
        if not doctor_info:
            raise serializers.ValidationError("无效的医生ID")
            
        # 验证时间段ID
        time_slot_info = get_time_slot_info(data['time_slot_id'])
        if not time_slot_info:
            raise serializers.ValidationError("无效的时间段ID")
            
        return data


class AppointmentSerializer(serializers.ModelSerializer):
    """预约信息序列化器"""
    id = serializers.CharField(read_only=True)
    
    class Meta:
        model = Appointment
        fields = '__all__'


class OrderSerializer(serializers.Serializer):
    """订单序列化器"""
    id = serializers.IntegerField(read_only=True)
    order_number = serializers.CharField(read_only=True)
    patient_id = serializers.IntegerField(read_only=True)
    doctor_id = serializers.IntegerField(read_only=True)
    appointment_id = serializers.IntegerField(read_only=True)
    amount = serializers.DecimalField(max_digits=10, decimal_places=2, read_only=True)
    status = serializers.CharField(read_only=True)
    payment_method = serializers.CharField(read_only=True, allow_null=True)
    payment_time = serializers.DateTimeField(read_only=True, allow_null=True)
    created_at = serializers.DateTimeField(read_only=True)
    updated_at = serializers.DateTimeField(read_only=True) 